package IO;

import java.nio.ByteBuffer;

public class NIODemo {
    public static void main(String[] args) {
        //创建一个字节缓冲区，申请内存空间为8字节
        ByteBuffer buf = ByteBuffer.allocate(8);
        System.out.println("position="+buf.position());
        System.out.println("limit="+buf.limit());
        System.out.println("capacity="+buf.capacity());
        System.out.println("----------------------");
        //向缓冲区写入put数据
        buf.put((byte) 10);
        buf.put((byte)20);
        buf.put((byte) 30);
        buf.put((byte) 40);
        System.out.println("position="+buf.position());
        System.out.println("limit="+buf.limit());
        System.out.println("capacity="+buf.capacity());
        System.out.println("-------------------");
        buf.flip();

        System.out.println("position="+buf.position());
        System.out.println("limit="+buf.limit());
        System.out.println("capacity="+buf.capacity());
//hasRemaining    告知在当前位置（position=0开始）和限制之间（limit）里面有无元素
        if(buf.hasRemaining()){
            //remaining 有多少元素
            for(int i=0;i< buf.remaining();i++){
                byte b = buf.get(i);
                System.out.println(b);
            }
        }

    }
}
